package com.jierui.meter.frontend.domain;

import com.jierui.meter.frontend.domain.DO.*;
import com.jierui.meter.frontend.domain.VO.*;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.SimpleFormatter;

/**
 * @description:
 * @author: wangjingjing
 * @creat: 2021-06-18 13:59
 */
public class BeanUtil {

    public static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    public static TErrorComputingAndMeterVO ErrorDO2VO(TErrorComputingAndMeterDO meterDO) {
        TErrorComputingAndMeterVO vo = new TErrorComputingAndMeterVO();
        vo.setId(meterDO.getId());
        vo.setOrg_name(meterDO.getOrgName().trim());
        vo.setOrg_7_name(meterDO.getOrg7Name().trim());
        vo.setMeter_id(meterDO.getMeterId());
        vo.setCons_no(meterDO.getConsNo());
        vo.setAssnet_no(meterDO.getAssnetNo());
        vo.setTg_id(meterDO.getTgId());
        vo.setTg_name(meterDO.getTgName().trim());
        if (meterDO.getWiringMode() != null) {
            vo.setMeter_phase(WiringModeEnum.getName(Integer.valueOf(meterDO.getWiringMode())));
        }
        vo.setCal_error(meterDO.getCalError());
        vo.setCheck_status(meterDO.getCheckStatus());
        vo.setCheck_status_reason(meterDO.getCheckStatusReason());
        vo.setError_e(meterDO.getErrorE());
        vo.setError_probability(meterDO.getErrorProbability());
        vo.setAlarm_level(meterDO.getAlarmCode());
        vo.setPriority_level(meterDO.getPriorityLevel());
        vo.setAlarm_code(meterDO.getAlarmCode());
        vo.setAlarm_name(ExceptionCodeEnum.getName(meterDO.getAlarmCode()));
        vo.setAlarm_level(meterDO.getAlarmLevel());
        vo.setAlarm_source(meterDO.getAlarmSource());
        return vo;
    }



    public static MeterArchiveInfoVO meterDO2VO(MeterArchiveInfoDO meterDO) {
        MeterArchiveInfoVO vo = new MeterArchiveInfoVO();
        vo.setMeter_id(meterDO.getMeterId());
        vo.setCons_id(meterDO.getConsId());
        vo.setMeter_no(meterDO.getMeterNo());
        vo.setMeter_assnet_no(meterDO.getMeterAssnetNo());
        vo.setSpec_code(meterDO.getSpecCode());
        vo.setPhase(meterDO.getPhase());
        vo.setCons_addr(meterDO.getConsAddr());
        return vo;
    }


    public static TgArchiveInfoVO tgArchiveDO2VO(TgInfoDO tgInfoDO) {
        TgArchiveInfoVO vo = new TgArchiveInfoVO();
        vo.setTg_no(tgInfoDO.getTGNO());
        vo.setTg_id(tgInfoDO.getTGID());
        vo.setTg_name(tgInfoDO.getTGNAME().trim());
        vo.setTg_addr(tgInfoDO.getTGADDR());
        vo.setTg_city(tgInfoDO.getTGCITY());
        vo.setTg_county(tgInfoDO.getTGCOUNTY());
        vo.setPlate_cap1(tgInfoDO.getPLATECAP());
        vo.setPlate_cap2(tgInfoDO.getPLATECAP());
        vo.setOrg_name(tgInfoDO.getORGNAME().trim());
        return vo;
    }


    public static MpInfoVO error2MpInfo(TErrorComputingResultsDO errorDO){
        MpInfoVO vo = new MpInfoVO();
        vo.setError_rate(errorDO.getErrorE());
        vo.setMeter_num(errorDO.getInvalidMeterNum());
        return vo;
    }


    public static ErrorInfoByMeterIDVO error2ErrorInfo(TErrorComputingResultsDO errorDO){
        ErrorInfoByMeterIDVO vo = new ErrorInfoByMeterIDVO();
        vo.setAlarm_code(errorDO.getAlarmCode());
        vo.setAlarm_time(errorDO.getDateDayzi());
        vo.setCheck_status(errorDO.getCheckStatus());
        return vo;
    }

    public static ErrorByOrgNameLikeVO tgInfo2ErrorNameLikeVO(TgInfoDO tgInfoDO){
        ErrorByOrgNameLikeVO vo = new ErrorByOrgNameLikeVO();
        if (null != tgInfoDO.getON7NAME()) {
            vo.setOrg_7_name(tgInfoDO.getORGNAME().trim());
        }
        if (null != tgInfoDO.getORGNAME()) {
            vo.setOrg_name(tgInfoDO.getORGNAME().trim());
        }
        if (null != tgInfoDO.getTGNAME()) {
            vo.setTg_name(tgInfoDO.getTGNAME().trim());
        }
        vo.setTg_id(tgInfoDO.getTGID());
        vo.setTg_status(TgStatusEnum.getName(tgInfoDO.getTGSTATUS()));
        vo.setPlate_cap(tgInfoDO.getPLATECAP());
        vo.setAlarm_time(format.format(tgInfoDO.getLASTDAY()));
        vo.setAlarm_type(TgStatusEnum.getName(tgInfoDO.getTGSTATUS()));
        vo.setAlarm_duration(tgInfoDO.getNDAYS());
        vo.setMeter_num(tgInfoDO.getNMETRES());
        return vo;
    }

    public static NewTgByNameLikeVO tgInfo2NewTgByNameLikeVO(TgInfoDO tgInfoDO){
        NewTgByNameLikeVO vo = new NewTgByNameLikeVO();
        if (null != tgInfoDO.getON7NAME()) {
            vo.setOrg_7_name(tgInfoDO.getON7NAME().trim());
        }
        if (null != tgInfoDO.getORGNAME()) {
            vo.setOrg_name(tgInfoDO.getORGNAME().trim());
        }
        if (null != tgInfoDO.getTGNAME()) {
            vo.setTg_name(tgInfoDO.getTGNAME().trim());
        }
        vo.setTg_id(tgInfoDO.getTGID());
        vo.setPlate_cap(tgInfoDO.getPLATECAP());
        vo.setErr_rate(tgInfoDO.getLOSSAVG());
        Calendar c = Calendar.getInstance();
        c.setTime(tgInfoDO.getLASTDAY());
        c.set(Calendar.DAY_OF_YEAR,0-tgInfoDO.getNDAYS());
        vo.setStart_time(format.format(c.getTime()));
        vo.setData_duration(tgInfoDO.getNDAYS());
        vo.setMeter_num(tgInfoDO.getNMETRES());
        return vo;
    }

    public static AllTgByNameLikeVO tgInfo2AllTgByNameLikeVO(TgInfoDO tgInfoDO){
        AllTgByNameLikeVO vo = new AllTgByNameLikeVO();
        if (null != tgInfoDO.getON7NAME()) {
            vo.setOrg_7_name(tgInfoDO.getORGNAME().trim());
        }
        if (null != tgInfoDO.getORGNAME()) {
            vo.setOrg_name(tgInfoDO.getORGNAME().trim());
        }
        if (null != tgInfoDO.getTGNAME()) {
            vo.setTg_name(tgInfoDO.getTGNAME().trim());
        }
        vo.setTg_id(tgInfoDO.getTGID());
        vo.setPlate_cap(tgInfoDO.getPLATECAP());
        vo.setErr_rate(tgInfoDO.getLOSSAVG());
        if (0 == tgInfoDO.getTGSTATUS()) {
            vo.setTg_status(TgStatusEnum.NEW.getName());
        } else {
            vo.setTg_status(TgStatusEnum.getName(tgInfoDO.getTGSTATUS()));
        }
        vo.setMeter_num(tgInfoDO.getNMETRES());
        return vo;
    }

    public static NormalTgByNameLikeVO tgInfo2NormalTgByNameLikeVO(TgInfoDO tgInfoDO){
        NormalTgByNameLikeVO vo = new NormalTgByNameLikeVO();
        if (null != tgInfoDO.getON7NAME()) {
            vo.setOrg_7_name(tgInfoDO.getORGNAME().trim());
        }
        if (null != tgInfoDO.getORGNAME()) {
            vo.setOrg_name(tgInfoDO.getORGNAME().trim());
        }
        if (null != tgInfoDO.getTGNAME()) {
            vo.setTg_name(tgInfoDO.getTGNAME().trim());
        }
        vo.setOrg_name(tgInfoDO.getORGNAME().trim());
        vo.setTg_id(tgInfoDO.getTGID());
        vo.setTg_name(tgInfoDO.getTGNAME().trim());
        vo.setPlate_cap(tgInfoDO.getPLATECAP());
        vo.setMeter_num(tgInfoDO.getNMETRES());
        vo.setErr_rate(tgInfoDO.getLOSSAVG());
        vo.setError2_meter_num(tgInfoDO.getNMETRES());
        return vo;
    }


    public static CalInfoVO showIndicesDO2CalInfoVO(ShowIndicesDO indicesDO) {
        CalInfoVO calInfoVO = new CalInfoVO();
        calInfoVO.setOrg_no(indicesDO.getOrgNo());
        calInfoVO.setOrg_name(indicesDO.getOrgName().trim());
        calInfoVO.setOrg_7_no(indicesDO.getOn7());
        calInfoVO.setOrg_7_name(indicesDO.getOrg7Name().trim());
        calInfoVO.setTg_num(indicesDO.getNTg());
        calInfoVO.setMeter_num(indicesDO.getNMeter());
        calInfoVO.setValid_tg_num(indicesDO.getNNormTg());
        calInfoVO.setNew_tg_num(indicesDO.getNNewTg());
        calInfoVO.setInvalid_tg_num(indicesDO.getNErrTg());
        //台区可算率  可算台区数/总台区
        // 可算台区 = 正常 + 异常
        if (indicesDO.getNTg() > 0) {
            float tg_rate = (int) Math.round((indicesDO.getNNormTg() + indicesDO.getNErrTg()) / (float) indicesDO.getNTg() * 10000) / 100F;
            calInfoVO.setValid_tg_rate(tg_rate);
        } else {
            calInfoVO.setValid_tg_rate(0f);
        }
        if (indicesDO.getNMeter() > 0) {
            //电能表可算率
            float meter_rate = Math.round((indicesDO.getNNormTgErrMeter() + indicesDO.getNNormTgNormMeter()) / (float) indicesDO.getNMeter() * 10000) / 100F;
            //电能表超差率
            float err_meter_rate = Math.round(indicesDO.getNNormTgErrMeter() / (float) indicesDO.getNMeter() * 10000) / 100F;
            calInfoVO.setValid_meter_rate(meter_rate);
            calInfoVO.setError_meter_rate(err_meter_rate);
        } else {
            calInfoVO.setValid_meter_rate(0f);
            calInfoVO.setError_meter_rate(0f);
        }
        return calInfoVO;
    }


    public static ResultTableVO showIndicesDO2ResultTableVO(ShowIndicesDO indicesDO) {
        ResultTableVO resultTableVO = new ResultTableVO();
        resultTableVO.setOrg_7_name(indicesDO.getOrg7Name().trim());
        //台区可算率  可算台区数/总台区
        // 可算台区 = 正常 + 异常
        if (indicesDO.getNTg() > 0) {
            float tg_rate = Math.round((indicesDO.getNNormTg() + indicesDO.getNErrTg()) / (float) indicesDO.getNTg() * 10000) / 100F;
            resultTableVO.setValid_tg_rate(tg_rate);
        } else {
            resultTableVO.setValid_tg_rate(0F);
        }
        if (indicesDO.getNMeter() > 0) {
            //电能表可算率
            float meter_rate = Math.round((indicesDO.getNNormTgErrMeter() + indicesDO.getNNormTgNormMeter()) / (float) indicesDO.getNMeter() * 10000F) / 100F;
            //电能表超差率
            float err_meter_rate = Math.round((indicesDO.getNNormTgErrMeter()) / (float) indicesDO.getNMeter() * 10000F) / 100F;
            resultTableVO.setValid_meter_rate(meter_rate);
            resultTableVO.setError_meter_rate(err_meter_rate);
        } else {
            resultTableVO.setValid_meter_rate(0F);
            resultTableVO.setError_meter_rate(0F);
        }
        return resultTableVO;
    }

    public static AllMeterByLikeVO meterStatus2AllMeterByLikeVO(MeterStatusDO meterStatus) {
        AllMeterByLikeVO vo = new AllMeterByLikeVO();
        vo.setOrg_no(meterStatus.getORGNO());
        vo.setOrg_7_no(meterStatus.getON7());
        if (StringUtils.isNotBlank(meterStatus.getON7NAME())) {
            vo.setOrg_7_name(meterStatus.getON7NAME().trim());
        }
        if (StringUtils.isNotBlank(meterStatus.getORGNAME())) {
            vo.setOrg_name(meterStatus.getORGNAME().trim());
        }
        vo.setMeter_id(meterStatus.getMETERID());
        vo.setCons_no(meterStatus.getCONSID());
        vo.setAssnet_no(meterStatus.getASSNETNO());
        vo.setMeter_type(MeterStatusEnum.getName(meterStatus.getSTATUS()));
        vo.setTg_id(meterStatus.getTGNO());
        if (StringUtils.isNotBlank(meterStatus.getTGNAME())) {
            vo.setTg_name(meterStatus.getTGNAME().trim());
        }
        return vo;
    }


}
